#include "util.h"

HRESULT D3DUCreateRhomb(IDirect3DDevice9* d3ddev, IDirect3DVertexBuffer9** rp_d3dvb, IDirect3DIndexBuffer9** rp_d3dib)
{
	/*
                          Y

						               Z

	                ****************0
	              * *     2       * *
   			    *****************   *
			    *   *      3    *   *
				* 5 *           * 1 *        X
			    *   *   7       *   *
			    *   *****************
				* *      6      * *
				4****************
	 
	 */
	//vertex
	Vertex vertices[6*4] = {
		//0127,++-
		Vertex(D3DXVECTOR3( 1.0f, 1.0f, 1.0f),D3DXVECTOR3( 1.0f, 1.0f,-1.0f),D3DXVECTOR2( 0.0f, 0.0f )),
		Vertex(D3DXVECTOR3( 1.0f, 0.0f, 0.0f),D3DXVECTOR3( 1.0f, 1.0f,-1.0f),D3DXVECTOR2( 1.0f, 0.0f )),
		Vertex(D3DXVECTOR3( 0.0f, 1.0f, 0.0f),D3DXVECTOR3( 1.0f, 1.0f,-1.0f),D3DXVECTOR2( 0.0f, 1.0f )),
		Vertex(D3DXVECTOR3( 0.0f, 0.0f,-1.0f),D3DXVECTOR3( 1.0f, 1.0f,-1.0f),D3DXVECTOR2( 1.0f, 1.0f )),

		//0235,-++
		Vertex(D3DXVECTOR3( 1.0f, 1.0f, 1.0f),D3DXVECTOR3(-1.0f, 1.0f, 1.0f),D3DXVECTOR2( 0.0f, 0.0f )),
		Vertex(D3DXVECTOR3( 0.0f, 1.0f, 0.0f),D3DXVECTOR3(-1.0f, 1.0f, 1.0f),D3DXVECTOR2( 1.0f, 0.0f )),
		Vertex(D3DXVECTOR3( 0.0f, 0.0f, 1.0f),D3DXVECTOR3(-1.0f, 1.0f, 1.0f),D3DXVECTOR2( 0.0f, 1.0f )),
		Vertex(D3DXVECTOR3(-1.0f, 0.0f, 0.0f),D3DXVECTOR3(-1.0f, 1.0f, 1.0f),D3DXVECTOR2( 1.0f, 1.0f )),

		//0316,+-+
		Vertex(D3DXVECTOR3( 1.0f, 1.0f, 1.0f),D3DXVECTOR3( 1.0f,-1.0f, 1.0f),D3DXVECTOR2( 0.0f, 0.0f )),
		Vertex(D3DXVECTOR3( 0.0f, 0.0f, 1.0f),D3DXVECTOR3( 1.0f,-1.0f, 1.0f),D3DXVECTOR2( 1.0f, 0.0f )),
		Vertex(D3DXVECTOR3( 1.0f, 0.0f, 0.0f),D3DXVECTOR3( 1.0f,-1.0f, 1.0f),D3DXVECTOR2( 0.0f, 1.0f )),
		Vertex(D3DXVECTOR3( 0.0f,-1.0f, 0.0f),D3DXVECTOR3( 1.0f,-1.0f, 1.0f),D3DXVECTOR2( 1.0f, 1.0f )),

		//4563,--+
		Vertex(D3DXVECTOR3(-1.0f,-1.0f,-1.0f),D3DXVECTOR3(-1.0f,-1.0f, 1.0f),D3DXVECTOR2( 0.0f, 0.0f )),
		Vertex(D3DXVECTOR3(-1.0f, 0.0f, 0.0f),D3DXVECTOR3(-1.0f,-1.0f, 1.0f),D3DXVECTOR2( 1.0f, 0.0f )),
		Vertex(D3DXVECTOR3( 0.0f,-1.0f, 0.0f),D3DXVECTOR3(-1.0f,-1.0f, 1.0f),D3DXVECTOR2( 0.0f, 1.0f )),
		Vertex(D3DXVECTOR3( 0.0f, 0.0f, 1.0f),D3DXVECTOR3(-1.0f,-1.0f, 1.0f),D3DXVECTOR2( 1.0f, 1.0f )),

		//4671,+--
		Vertex(D3DXVECTOR3(-1.0f,-1.0f,-1.0f),D3DXVECTOR3( 1.0f,-1.0f,-1.0f),D3DXVECTOR2( 0.0f, 0.0f )),
		Vertex(D3DXVECTOR3( 0.0f,-1.0f, 0.0f),D3DXVECTOR3( 1.0f,-1.0f,-1.0f),D3DXVECTOR2( 1.0f, 0.0f )),
		Vertex(D3DXVECTOR3( 0.0f, 0.0f,-1.0f),D3DXVECTOR3( 1.0f,-1.0f,-1.0f),D3DXVECTOR2( 0.0f, 1.0f )),
		Vertex(D3DXVECTOR3( 1.0f, 0.0f, 0.0f),D3DXVECTOR3( 1.0f,-1.0f,-1.0f),D3DXVECTOR2( 1.0f, 1.0f )),

		//4752.-+-
		Vertex(D3DXVECTOR3(-1.0f,-1.0f,-1.0f),D3DXVECTOR3(-1.0f, 1.0f,-1.0f),D3DXVECTOR2( 0.0f, 0.0f )),
		Vertex(D3DXVECTOR3( 0.0f, 0.0f,-1.0f),D3DXVECTOR3(-1.0f, 1.0f,-1.0f),D3DXVECTOR2( 1.0f, 0.0f )),
		Vertex(D3DXVECTOR3(-1.0f, 0.0f, 0.0f),D3DXVECTOR3(-1.0f, 1.0f,-1.0f),D3DXVECTOR2( 0.0f, 1.0f )),
		Vertex(D3DXVECTOR3( 0.0f, 1.0f, 0.0f),D3DXVECTOR3(-1.0f, 1.0f,-1.0f),D3DXVECTOR2( 1.0f, 1.0f ))
	};
	//transform
	D3DXMATRIX dxmatRot1;
	D3DXMatrixRotationY(&dxmatRot1,acosf(1.0f/sqrtf(2.0f)));
	D3DXMATRIX dxmatRot2;
	D3DXMatrixRotationZ(&dxmatRot2,acosf(1.0f/sqrtf(3.0f)));
	D3DXMATRIX dxmatRot;
	D3DXMatrixMultiply(&dxmatRot,&dxmatRot1,&dxmatRot2);
	for(int i=0; i<24; i++){
		D3DXVECTOR4 rst;
		D3DXVec4Transform(&rst,&D3DXVECTOR4(vertices[i].v3Pos,1.0),&dxmatRot);
		vertices[i].v3Pos = D3DXVECTOR3(rst.x,rst.y,rst.z);
		D3DXVec4Transform(&rst,&D3DXVECTOR4(vertices[i].v3Norm,0.0),&dxmatRot);
		D3DXVec4Normalize(&rst,&rst);
		vertices[i].v3Norm = D3DXVECTOR3(rst.x,rst.y,rst.z);
	}
	//index
	WORD indeces[12*3] = {
		0,	1,	2, 
		3,	2,	1, 
		4,	5,	6, 
		7,	6,	5, 
		8,	9,	10, 
		11,	10,	9, 
		14,	13,	12, 
		13,	14,	15, 
		18,	17,	16, 
		17,	18,	19, 
		22,	21,	20, 
		21,	22,	23
	};

	void* buf = NULL;
	
	HR(d3ddev->CreateVertexBuffer(24*sizeof(Vertex),D3DUSAGE_WRITEONLY,NULL,D3DPOOL_MANAGED,rp_d3dvb,NULL));
	(*rp_d3dvb)->Lock(0,0,&buf,0x00000000);
	memcpy(buf,vertices,24*sizeof(Vertex));
	(*rp_d3dvb)->Unlock();

	HR(d3ddev->CreateIndexBuffer(36*sizeof(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,rp_d3dib,NULL));
	(*rp_d3dib)->Lock(0,0,&buf,0x00000000);
	memcpy(buf,indeces,36*sizeof(WORD));
	(*rp_d3dib)->Unlock();

	return D3D_OK;
}